import * as echarts from 'echarts'
import { getHtmlFontPX } from './base'
export default function newChart(type, _this) {
  const seriesObj = {
    type: type,
    barMaxWidth: _this.barMaxWidth,
    radius: _this.radius || [],
    center: _this.center || [],
    roseType: _this.roseType || '',
    lineStyle: {
      width: _this.lineStyleWidth
    },
    itemStyle: type === 'bar' ? {
      barBorderRadius: _this.barBorderRadius, // 圆角（左上、右上、右下、左下）
      color: function(params) {
        return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
          {
            offset: 0,
            color: _this.colorList[params.seriesIndex].c1
          },
          {
            offset: 1,
            color: _this.colorList[params.seriesIndex].c2
          }
        ])
      }
    } : {}
  }
  return {
    title: {
      text: _this.title,
      textStyle: _this.textStyle,
      top: '1%',
      left: 'center'
    },
    color: type !== 'bar' ? _this.colorList.map(item => {
      return new echarts.graphic.LinearGradient(1, 0, 0, 0, [
        {
          offset: 0,
          color: item.c1
        },
        {
          offset: 1,
          color: item.c2
        }
      ])
    }) : '',
    legend: {
      show: _this.showLegend || false
    },
    tooltip: {
      show: true,
      trigger: _this.tooltip || 'item',
      axisPointer: {
        type: 'line'
      },
      backgroundColor: _this.tooltipBackgroundColor, // 设置背景颜色
      borderColor: 'transparent',
      borderWidth: 0,
      textStyle: {
        color: _this.tooltipFontColor,
        fontSize: getHtmlFontPX(_this.fontSize)
      }
    },
    grid: {
      top: _this.top,
      right: _this.right,
      left: _this.left,
      bottom: _this.bottom,
      containLabel: true
    },
    xAxis: type !== 'pie' ? {
      type: 'category',
      data: _this.barData.xAxisData || [],
      // x轴文字
      axisLabel: {
        textStyle: {
          color: _this.axisLabelColor,
          fontSize: getHtmlFontPX(_this.fontSize)
        },
        rotate: _this.rotate,
        margin: _this.axisLabelMargin
      },
      // x轴
      axisLine: {
        show: true,
        lineStyle: {
          width: 1,
          color: _this.axisLineColor
        }
      },
      // x轴刻度
      axisTick: {
        show: false
      }
    } : '',
    yAxis: type !== 'pie' ? {
      type: 'value',
      // y轴 文字
      axisLabel: {
        textStyle: {
          fontSize: getHtmlFontPX(_this.fontSize),
          color: _this.axisLabelColor
        }
      },
      // y轴
      axisLine: {
        show: false
      },
      // y轴横向 标线
      splitLine: {
        show: false
      }
    } : '',
    series: type !== 'pie' ? _this.barData.seriesData.map(item => {
      return {
        name: item.name || '',
        data: item.value || [],
        ...seriesObj
      }
    }) : {
      data: _this.barData.seriesData || [],
      ...seriesObj
    }

  }
}
